package com.hmsproject.search.managedbean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.map.LatLng;

import com.hmsproject.common.HMSManagedBeans;
import com.hmsproject.common.HMSUtil;
import com.hmsproject.datastructures.Hotel;
import com.hmsproject.db.HMSDBConnectionFactory;
import com.hmsproject.db.inmemorydb.DealMap;
import com.hmsproject.search.bean.HotelInfo;
import com.hmsproject.search.bean.SearchBean;
import com.mymobile.showcase.mobile.SignInUserView;

@ManagedBean
@SessionScoped
public class SearchView {

	private SearchBean searchBean;

	private String selectedHotelId;

	protected List<HotelInfo> hotels;

	private String eventId;

	@ManagedProperty(value = "#{dealView}")
	private DealView dealView;
	
	@ManagedProperty(value = "#{signInUserView}")
	private SignInUserView signInUserView;


	public List<HotelInfo> getHotels() {
		return hotels;
	}
	
	public Hotel getHotel(LatLng latlng)
	{
		for (HotelInfo hotelInfo : this.hotels) {
			if(hotelInfo.getHotel().getLalititude().equals(latlng.getLat()) && hotelInfo.getHotel().getLongtitude().equals(latlng.getLng())){
				return hotelInfo.getHotel();
			}
		}
		
		return null;
	}

	public void setHotels(List<HotelInfo> hotels) {
		this.hotels = hotels;
	}

	public String getEventId() {
		return eventId;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	public SearchBean getSearchBean() {
		return searchBean;
	}

	public void setSearchBean(SearchBean searchBean) {
		this.searchBean = searchBean;
	}

	public DealView getDealView() {
		return dealView;
	}

	public void setDealView(DealView dealView) {
		this.dealView = dealView;
	}

	public SignInUserView getSignInUserView() {
		return signInUserView;
	}

	public void setSignInUserView(SignInUserView signInUserView) {
		this.signInUserView = signInUserView;
	}

	@PostConstruct
	public void init() {
		this.searchBean = new SearchBean();
	}

	public String getCurrentMilliSecd() {
		return new Date(System.currentTimeMillis()).toString();
	}

	public String getSelectedHotelId() {
		return selectedHotelId;
	}

	public void setSelectedHotelId(String selectedHotelId) {
		this.selectedHotelId = selectedHotelId;
	}

	

	public void searchValuesFromExternal() {

		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String, String> params = fc.getExternalContext()
				.getRequestParameterMap();
		if (params.containsKey("placeName")) {
			System.out.println(params.get("fromDate") + " "
					+ params.get("toDate") + " " + params.get("placeName") );
			this.getSearchBean().getPlaceBean()
					.setPlaceName(params.get("placeName"));
			this.getSignInUserView().setEmailAddress(params.get("name"));
			this.getSignInUserView().setUserLoggedIn(true);
			try {
				this.getSearchBean().getPlaceBean()
						.setFromDate(formatter.parse(params.get("fromDate")));
				this.getSearchBean().getPlaceBean()
						.setFromDate(formatter.parse(params.get("toDate")));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public String search() {
		// this.hotels =
		// HMSDBConnectionFactory.getInstance().getDBConnection().searchHotels(this.searchBean);
		this.searchValuesFromExternal();
		this.searchBean.setEventId(this.eventId);
		System.out.println("CCALLING SEARCH BEAN  " + this.searchBean);
		this.hotels = HMSDBConnectionFactory.getInstance().getDBConnection()
				.searchHotelsExpedia(this.searchBean);
		System.out.println(this.searchBean.getFilterBean().isDealOnly());
		/*
		 * if(this.searchBean.getFilterBean().isDealOnly()){ this.hotels =
		 * this.hotels.stream().filter(a ->
		 * a.hasDeal()).collect(Collectors.toList()); }
		 */
		System.out.println(hotels);
		return "/search/search.xhtml?faces-redirect=true";
	}

	public String gotoViewDeal(Hotel hotels) {
		this.selectedHotelId = hotels.getHotelId();
		this.dealView.setDealId(DealMap.INSTANCE.getDealForHotel(
				this.selectedHotelId).getDealId());
		this.dealView.init();
		return "/viewHotel.xhtml?faces-redirect=true";
	}

	public String getStar(int star) {

		StringBuilder startString = new StringBuilder();

		for (int i = 0; i < star; i++) {
			startString.append("<i class=\"fa fa-star\"></i>");
		}
		return startString.toString();

	}

	public String addToWatchList() {
		boolean isUserLoggedIn = HMSManagedBeans.getHMSManagedBeans()
				.isUserLoggedIn();

		System.out.println("IS USER LOGGED IN " + isUserLoggedIn);

		if (!isUserLoggedIn) {
			return HMSManagedBeans.getHMSManagedBeans().redirectToSignPage(
					"/deal/viewHotel.xhtml?dealId=" + this.dealView.getDealId()
							+ "&");
		}

		this.dealView.setDealData(HMSUtil.getInstance().buyDeal(
				this.dealView.getDealData(),
				HMSManagedBeans.getHMSManagedBeans().getEmailAddress()));
		return "/deal/addToWatchList.xhtml?faces-redirect=true";
	}

	public String buyAction() {
		boolean isUserLoggedIn = HMSManagedBeans.getHMSManagedBeans()
				.isUserLoggedIn();

		System.out.println("IS USER LOGGED IN " + isUserLoggedIn);

		if (!isUserLoggedIn) {
			return HMSManagedBeans.getHMSManagedBeans().redirectToSignPage(
					"/deal/viewHotel.xhtml?dealId=" + this.dealView.getDealId()
							+ "&");
		}

		this.dealView.setDealData(HMSUtil.getInstance().buyDeal(
				this.dealView.getDealData(),
				HMSManagedBeans.getHMSManagedBeans().getEmailAddress()));
		return "/deal/buyDeal.xhtml?faces-redirect=true";
	}

}
